package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.enums.ConsumptiontStatusEnum;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"c.status"); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Override
    public Consumption save(String ano) {
        Date date = new Date();
        //通过流水单查询预约单信息
        Appointment appointment=appointmentService.selectByAno(ano);
        //把状态改为消费中
        appointmentService.updateStatus(appointment.getId(), AppointmentStatusEnum.CONSUME.getValue());
        //生成结算单
        Consumption consumption = new Consumption();
        String format = DateUtil.format(date, "yyyyMMddHHmmss");
        String numbers = RandomUtil.randomNumbers(5);
        consumption.setCno(format+numbers);
        //设置为待结算
        consumption.setStatus(ConsumptiontStatusEnum.CONSUME.getValue());
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        //关联流水单
        consumption.setAppointmentAno(ano);
        //设置进店时间
        consumption.setCheckinTime(date);
        //设置消费萌点
        consumption.setBusiness(appointment.getBusiness());
        //设置创建时间
        consumption.setCreateTime(date);
        //return consumptionMapper.save(ano);
        consumption.setCreateid(UserContext.getCurrentUser());
        consumptionMapper.insert(consumption);
        return consumption;
    }


}
